<?php
/**
 * @author 董齐
 * @version 1.01
 */


/*
 * 输出数组或变量调试内容
 */
function p() {
	$argvs = func_get_args();
	echo "<div style=\"text-align: left;\">\r\n";
	foreach ($argvs as $v) {
		echo "<xmp>";
		print_r($v);
		echo "</xmp>\r\n";
	}
	echo "</div>\r\n";
}

/*
 * 输出类变量调试内容
*/
function v() {
	$pare = func_get_args();
	foreach ($pare as $v) {
		echo "<xml>";
		var_dump($v);
		echo "</xml>";
	}
}

/*
 * 页面重定向
 */
function redirection($page = 'user')
{
	header("location:?act={$page}");
	die;
}

/*
 * 获取格式化的当前时间
 */
function get_now()
{
	return date("Y-m-d H:i:s",time());
}

/*
 * 检查登陆状态 未登录或登陆过期则跳转到登陆界面
 * 登陆过期时间为24小时，无操作登出时间为20分钟
 */
function check_login()
{
	if (empty($_SESSION['admin']['user_name'])) {
		redirection();
		die();
	}
	if (time() - $_SESSION['admin']['login_time'] > 24*3600 || time() - $_SESSION['admin']['last_act_time'] > 30*60)
	{
		session_destroy();
		redirection();
		die();
	}
	$_SESSION['admin']['last_act_time'] = time();
}

/*
 * 格式化函数返回信息
 */
function return_info($error = true, $value = null)
{
	if ($error) {
		return array(
			'success' => true,
			'ret' => $value
			);
	} else {
		return array(
			'success' => false,
			'msg' => $value
			);
	}
}

function error_404()
{
	header("HTTP/1.1 404 Not Found");
	die;
}

/*
 * 通过parent_id调整数组结构为树形
 */
function build_tree($array)
{
	$ret = array();
	foreach ($array as $id => $value) {
		if (empty($value['parent_id'])) {
			$ret[] = _build_tree($array, $id, $value);
		}
	}
	return $ret;
}

function _build_tree($array, $fa_id, $father)
{
	$father['child'] = array();
	foreach ($array as $id => $value) {
		if ($value['parent_id'] == $fa_id) {
			$father['child'][] = _build_tree($array, $id, $value);
		}
	}
	return $father;
}

function add_quote($list)
{
	foreach($list as $k => $v) {
		$list[$k] = "'{$v}'";
	}
	return $list;
}

function export_excel($file_name,$excel)
{
	header("Content-Type: application/force-download");
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/download");
	header('Content-Disposition:inline;filename="'.$file_name.'.xls"');
	header("Content-Transfer-Encoding: binary");
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Pragma: no-cache");
	$objWriter = new PHPExcel_Writer_Excel5($excel);
	$objWriter->save('php://output');
}
?>